﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YT.DB;
using YT.Modules;
using YT.Commons;

namespace YT.XH.NOSE
{
    public partial class frmNSBrowFurtureSendMsg : Form
    {
        private bool mblnUpdate = false;
        private DBAccess mdbcon = new DBAccess(false, false);
        public frmNSBrowFurtureSendMsg()
        {
            InitializeComponent();
        }

        private void frmNSBrowFurtureSendMsg_Load(object sender, EventArgs e)
        {
            this.Text = "待发送信息维护";
            FillGrid();
        }


        private void FillGrid()
        {
            string strSQL = string.Empty;
            strSQL = "select * from SmsOutbox where SendSuccess=0 order by SendDate ";
            DataTable dtMsg = mdbcon.GetDataTable(strSQL);
            foreach (DataRow drRow in dtMsg.Rows)
            {
                this.dgvAll.Rows.Add();
                this.dgvAll.Rows[this.dgvAll.Rows.Count - 1].Cells[colRecID.Index].Value = CvtFunc.ToString(drRow["RecID"]);
                this.dgvAll.Rows[this.dgvAll.Rows.Count - 1].Cells[AllSickName.Index].Value = CvtFunc.ToString(drRow["SickName"]);
                this.dgvAll.Rows[this.dgvAll.Rows.Count - 1].Cells[AllSickRecID.Index].Value = CvtFunc.ToString(drRow["SickRecID"]);
                this.dgvAll.Rows[this.dgvAll.Rows.Count - 1].Cells[AllMobileNo.Index].Value = CvtFunc.ToString(drRow["MobilePhone"]);
                this.dgvAll.Rows[this.dgvAll.Rows.Count - 1].Cells[AllMsg.Index].Value = CvtFunc.ToString(drRow["Message"]);
                this.dgvAll.Rows[this.dgvAll.Rows.Count - 1].Cells[AllDate.Index].Value = BaseFunctions.FormatDate(CvtFunc.ToDateString(drRow["SendDate"]), DateFormatOption.DateFmtYMD);
            }
        }


        private bool ValidCheck( )
        {
            if (dgvAll.CurrentRow == null) return false ;
            DataGridViewRow dgvRow = this.dgvAll.CurrentRow;
            if (!BaseFunctions.IsDate(dgvRow.Cells[AllDate.Index].Value))
            {
                MessageBox.Show("错误的日期.", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else if (CvtFunc.ToString(dgvRow.Cells[AllMsg.Index].Value).Length > 200)
            {
                MessageBox.Show("发送的信息不能长于200。", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false ;
            }
            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            mblnUpdate = true;
            if ( !UpdateData()) return ;
           
            this.Close();
        }

        private bool  UpdateData()
        {
            this.dgvAll.EndEdit();
            if (!ValidCheck()) return false;
            string strSQL = string.Empty;
            string strRecID = string.Empty;
            string strMsg = string.Empty;
            DateTime datSendDate;
            foreach (DataGridViewRow dgvRow in this.dgvAll.Rows)
            {
                datSendDate = CvtFunc.ToDateTime(dgvRow.Cells[AllDate.Index].Value);
                strRecID = CvtFunc.ToString(dgvRow.Cells[colRecID.Index].Value);
                strMsg = CvtFunc.ToString ( dgvRow.Cells [AllMsg.Index].Value);
                strSQL = "update SmsOutbox set SendDate='{1}', Message='{2}' where RecID='{0}'";
                strSQL = string.Format(strSQL, strRecID   , datSendDate,strMsg);
                mdbcon.ExecuteSql(strSQL);
            }
            return true;
        }

        private void dgvAll_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dgvAll.CurrentRow == null) return;
            if (e.ColumnIndex == colDel.Index)
            {
                if (MessageBox.Show("您确定要删除此条记录？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    string strSQL = string.Empty;
                    string strRecID = CvtFunc.ToString(this.dgvAll.CurrentRow.Cells[colRecID.Index].Value);
                    strSQL = "delete SmsOutbox where RecID='" + strRecID + "'";
                    mdbcon.ExecuteSql(strSQL);
                    this.dgvAll.Rows.Remove ( this.dgvAll.CurrentRow );
                }
            }
        }

        private void frmNSBrowFurtureSendMsg_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!mblnUpdate)
            {
                if (MessageBox.Show("是否保存？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (!UpdateData()) return;
                }
            }
            mdbcon.CloseCon(); 
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvAll_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (!ValidCheck()) e.Cancel = true;
        }
    }
}
